<?php

/**
 * TopAdmin
 * 版权所有 2018-2027 zyncms，并保留所有权利。
 * Author: 御宅男 <530765310@qq.com>
 * Date: 2021/2/24
 */
namespace app\admin\controller\config;

use app\admin\model\AuthDept;
use app\admin\model\Config as ConfigModel;
use app\common\controller\Backend;
use app\common\model\ConfigGroup as ConfigGroupModel;
use app\common\traits\Curd;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Config extends Backend
{
    use Curd;
    public $banfie;
    protected $modelClass = null;

    public function initialize()
    {
        parent::initialize();
        //允许使用的字段列表
        $this->banfie = ["text", "checkbox", "textarea", "radio", "number", "datetime", "image", "images", "array", "switch", "select", "selects", "selectpage", "Ueditor", "file", "files", 'color', 'tags', 'markdown', 'city', 'custom'];
        $this->modelClass = new ConfigModel;
    }

    //配置首页
    public function index($group = 'base')
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $list =  $this->modelClass->alias('u')
                ->where($where)->where('group', $group)
                ->join('field_type ft','u.type = ft.name') //此处表别名不能加as
                ->field('ft.name as fieldName,u.*')
                ->order('sort,id desc')
                ->page($page, $limit)
                ->select();
            $total = $this->modelClass->where($where)->where('group', $group)->count();
            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);
        } else {
            $groupArray = Db::name('config_group')->column('name,title', 'name');
            $view = [
                'groupArray' => $groupArray,
                'group' => $group,
            ];
            return view('',$view);
        }
    }


    //新增配置
    public function add()
    {
        if ($this->request->isPost()) {
            $data           = $this->request->post();
            $data['status'] = isset($data['status']) ? intval($data['status']) : 1;
            $result         = $this->validate($data, 'Config');
            if (true !== $result) {
                return $this->error($result);
            }
            if (ConfigModel::create($data)) {
                cache('Config', null); //清空缓存配置
                $this->success('配置添加成功~', url('index'));
            } else {
                $this->error('配置添加失败！');
            }
        } else {
            $fieldType = Db::name('field_type')->where('name', 'in', $this->banfie)->order('sort')->column('name,title,isoption,isstring');
            $groupArray = Db::name('config_group')->column('name,title', 'name');
            $view = [
                'groupArray' => $groupArray,
                'fieldType' => $fieldType,
                'info' => '',
            ];
            return view('',$view);
        }
    }

    //编辑配置
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            try {
                $this->validate($data, 'Config');
                ConfigModel::update($data);
                cache('Config', null); //清空缓存配置
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success(lang('编辑成功'), url('system.config/index'));
        } else {
            $id = $this->request->param('id/d');
            if (!is_numeric($id) || $id < 0) {
                return '参数错误';
            }
            $fieldType = Db::name('field_type')->where('name', 'in', $this->banfie)->order('sort')->column('name,title,isoption,isstring');
            $info = ConfigModel::find($id);
            $groupArray = Db::name('config_group')->column('name,title', 'name');
            $view = [
                'groupArray' => $groupArray,
                'fieldType' => $fieldType,
                'info' => $info,
            ];
            return view('',$view);
        }
    }

    //删除配置
    public function del()
    {
        cache('Config', null); //清空缓存配置
        return parent::del();
    }

    public function multi()
    {
        cache('Config', null); //清空缓存配置
        return parent::multi();
    }

}
